import { ElMessage, ElMessageBox } from 'element-plus';

const base = (msg: string) => ({
  duration: 4000,
  showClose: true,
  message: msg,
  dangerouslyUseHTMLString: true,
});

export default class Message {
  static error(msg: string) {
    return ElMessage({
      type: 'error',
      ...base(msg),
    });
  }

  static success(msg: string) {
    return ElMessage({
      type: 'success',
      ...base(msg),
    });
  }

  static warning(msg: string) {
    return ElMessage({
      type: 'warning',
      ...base(msg),
    });
  }

  static info(msg: string) {
    return ElMessage({
      type: 'info',
      ...base(msg),
    });
  }

  static confirm(msg: string, resovleFn: () => void) {
    return ElMessageBox.confirm(msg, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
      .then(() => {
        resovleFn();
      })
      .catch(() => Message.info('哦，原来是点错啦！'));
  }

  static delete(msg: string, resovleFn: () => void) {
    return ElMessageBox.confirm(msg, '删除', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
      .then(() => {
        resovleFn();
      })
      .catch(() => Message.info('哦，原来是点错啦！'));
  }
}
